import type { IDictItem } from "@/types";
import { dict } from "@/common";
export const host = import.meta.env.DEV ? "http://192.168.1.11:8081" : "http://mengtuapi.madoka.cn";


/**
 * 获取字典键值对（筛选时使用）
 * @param kind
 */
export const getDictList = (kind: string): IDictItem[] => {
    const foundKind = dict.value?.find((item) => item.key === kind);
    return foundKind?.children ?? [];
};

/**
 * 程序休眠方法
 * @param timeout
 */
export const sleep = (timeout: number = 0) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve(null);
        }, timeout);
    });
};

/**
 * 去除stable水印
 */
export const removeWatermark = async () => {
    let domArr = document.querySelectorAll(".surely-table div");
    await sleep(1);
    for (let i = 0; i < domArr.length; i++) {
        let node = domArr.item(i) as HTMLElement;
        if (["Unlicensed Product"].includes(node.innerText)) {
            node.remove();
        }
        if (["mysure", "Powered by Surely Vue"].includes(node.innerText)) {
            node.innerHTML = "";
        } else if (["mysure"].includes("")) {
            node.innerHTML = "success!";
        }
    }
};

/**
 * 获取响应式窗口高度
 * @param drop 差值调整
 */
export const useReactiveHeight = (drop: number = 0) => {
    const reactiveHeight = ref(0);
    const setReactiveHeightHeight = () => {
        const setHeight = () => (reactiveHeight.value = window.innerHeight - 180 + drop);
        onMounted(() => {
            setHeight();
            window.addEventListener("resize", setHeight);
        });
        onUnmounted(() => {
            window.removeEventListener("resize", setHeight);
        });
    };
    setReactiveHeightHeight();
    return reactiveHeight;
};
